package net.kleinhenz.questor.model;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Commit;

@Root
public class Quest
{
	@Attribute(required=true)
	private int start = -1;

	@Attribute(required=true)
	private String rules = null;

	@Attribute(required=true)
	private int qsrversion = -1;

	@Element(required=true)
	private About about = null;

	@Element(required=true)
	private Player player = null;

	@ElementList(required=false)
	private List<Item> items = new ArrayList<Item>();

	@ElementList(required=false)
	private List<Foe> foes = new ArrayList<Foe>();

	@ElementList(required=true)
	private List<Station> stations = new ArrayList<Station>();

	private Station root = null;

	public Player getPlayer()
	{
		return player;
	}

	public void setPlayer(Player player)
	{
		this.player = player;
	}

	public Station getRoot()
	{
		return root;
	}
	
	public void setRoot(Station root)
	{
		this.root = root;
	}
	
	public List<Foe> getFoes()
	{
		return foes;
	}
	
	public void setFoes(List<Foe> foes)
	{
		this.foes = foes;
	}

	public List<Item> getItems()
	{
		return items;
	}
	
	public void setItems(List<Item> items)
	{
		this.items = items;
	}
	
	public List<Station> getStations()
	{
		return stations;
	}
	
	public void setStations(List<Station> stations)
	{
		this.stations = stations;
	}

	public String getRules() {
		return rules;
	}

	public void setRules(String rules) {
		this.rules = rules;
	}
	
	public int getQsrversion() {
		return qsrversion;
	}

	public void setQsrversion(int qsrversion) {
		this.qsrversion = qsrversion;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setAbout(About about) {
		this.about = about;
	}

	public About getAbout() {
		return about;
	}

	public Station findStation(int stationId)
	{
		for (Station s : getStations())
				if (s.getId()==stationId)
					return s;

		return null;
	}

	public Choice findChoice(String choiceId)
	{
		for (Station s : getStations())
			for (Choice c : s.getChoices())
				if (c.getId().equals(choiceId))
					return c;

		return null;
	}
	
	public Foe findFoe(int foeId) {
		for (Foe f : getFoes())
			if (f.getId()==foeId)
				return f;
		return null;
	}
	
	@Commit
	public void findStart() {
		setRoot(findStation(getStart()));
	}
}
